package com.tansun.easycare.rule.rulegroup.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tansun.easycare.rule.rulegroup.entity.RuleEntry;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;

/**
 *  复杂规则集保存信息
 * @author xuconghui
 *
 */
public class ComplexRuleGroup {

	/**
	 * 复杂规则集信息
	 */
	private RuleManage ruleManage;
	
	/**
	 * 普通条目信息
	 */
    private List<RuleEntryEntity> generalEntrys;
    
    /**
     * 互斥条目信息
     */
    private List<RuleEntryEntity> mutualExclusions;

    
	public ComplexRuleGroup(RuleManage ruleManage, List<RuleEntry> ruleEntryList, List<RuleEditEntity> ruleEntitys) {
		super();
		this.ruleManage = ruleManage;
		this.generalEntrys = new ArrayList<RuleEntryEntity>();
		this.mutualExclusions = new ArrayList<RuleEntryEntity>();
		
		Map<String,List<RuleEditEntity>> map = new HashMap<String,List<RuleEditEntity>>();
		for(RuleEditEntity entity: ruleEntitys) {
			List<RuleGroup> rgList = entity.getRuleGroups();
			if(rgList !=null && !rgList.isEmpty()) {
				RuleGroup rg = rgList.get(0);
				String entryId = rg.getEntryId();
				List<RuleEditEntity> entityList = null;
				if(map.containsKey(entryId)) {
					entityList = map.get(entryId);
				}else {
					entityList = new ArrayList<>();
					map.put(entryId, entityList);
				}
				entityList.add(entity);
			}
		}
        Comparator<RuleEditEntity> comparator1 = new Comparator<RuleEditEntity>() {
			public int compare(RuleEditEntity re1, RuleEditEntity re2) {
					List<RuleGroup> rgList1 = re1.getRuleGroups();
					List<RuleGroup> rgList2 = re2.getRuleGroups();
					int p1 = 0;
					int s1 = 0;
					int p2 = 0;
					int s2 = 0;
					if(rgList1 != null && !rgList1.isEmpty()) {
						RuleGroup r1 = rgList1.get(0);
						p1 = r1.getPriority()==null?0:Integer.parseInt(r1.getPriority());
						s1 = r1.getSort()==null?0:r1.getSort();
					}

					if(rgList2 != null && !rgList2.isEmpty()) {
						RuleGroup r2 = rgList2.get(0);
						p2 = r2.getPriority()==null?0:Integer.parseInt(r2.getPriority());
						s2 = r2.getSort()==null?0:r2.getSort();
					}
					return (p1-p2)*10000-(s1-s2);
			}
		};
		for(RuleEntry ruleEntry:ruleEntryList) {
			String groupType = ruleEntry.getGroupType();
			String entryId = ruleEntry.getId();
			List<RuleEditEntity> rules = map.get(entryId);
			if(rules!=null &&rules.size()>1) {
        		if(RuleEntry.ACCESSMODEL_PRIORITY_2.equals(ruleEntry.getAccessMode())){//优先级准入
        			Collections.sort(rules,comparator1);
                }
			}
			RuleEntryEntity ruleEntityEntry = new RuleEntryEntity(ruleEntry,rules);
			if(RuleEntry.GROUPTYPE_GENERAL_0.equals(groupType)) {
				this.generalEntrys.add(ruleEntityEntry);
			}else if(RuleEntry.GROUPTYPE_MUTEXCLU_1.equals(groupType)) {
				this.mutualExclusions.add(ruleEntityEntry);
			}
		}
        Comparator<RuleEntryEntity> comparator = new Comparator<RuleEntryEntity>() {
			public int compare(RuleEntryEntity r1, RuleEntryEntity r2) {
					RuleEntry re1 = r1.getRuleEntry();
					RuleEntry re2 = r2.getRuleEntry();
					return re1.compareTo(re2);
			}
		};
		if(this.generalEntrys.size()>1) {
			Collections.sort(this.generalEntrys,comparator);
		}
		if(this.mutualExclusions.size()>1) {
			Collections.sort(this.mutualExclusions,comparator);
		}
	}

	public RuleManage getRuleManage() {
		return ruleManage;
	}

	public void setRuleManage(RuleManage ruleManage) {
		this.ruleManage = ruleManage;
	}

	public List<RuleEntryEntity> getGeneralEntrys() {
		return generalEntrys;
	}

	public void setGeneralEntrys(List<RuleEntryEntity> generalEntrys) {
		this.generalEntrys = generalEntrys;
	}

	public List<RuleEntryEntity> getMutualExclusions() {
		return mutualExclusions;
	}

	public void setMutualExclusions(List<RuleEntryEntity> mutualExclusions) {
		this.mutualExclusions = mutualExclusions;
	}
	
}
